<template>
  <div v-loading="loading" class="app-container">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item name="1">
        <template slot="title">
          <i class="el-icon-search" />搜索栏
        </template>
        <div class="head-container">
          <el-form class="searchform" :model="params" :inline="true" size="mini" label-width="auto">
            <!-- 字段变更 -->
            <div v-if="$route.name === 1">
              <el-form-item label="流水号">
                <el-input v-model="params.vendorPortalId" size="mini" placeholder="输入流水号" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="条码">
                <el-input v-model="params.upcNbr" size="mini" placeholder="输入条码搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="商品号">
                <el-input v-model="params.itemNbr" size="mini" placeholder="输入商品号搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="部门">
                <el-input v-model="params.deptNbr" size="mini" placeholder="输入部门搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="供应商编号">
                <el-input v-model="params.vendorNbr" size="mini" placeholder="输入供应商编号搜索" clearable style="width: 200px;" />
              </el-form-item>
            </div>
            <!-- 商品交叉 -->
            <div v-if="$route.name === 2">
              <el-form-item label="主商品">
                <el-input v-model="params.mainItemNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="次商品">
                <el-input v-model="params.secondItemNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="作业类型">
                <el-select v-model="params.workOption" size="mini" placeholder="选择搜索" clearable style="width: 200px">
                  <el-option key="1" label="连交叉" value="1" />
                  <el-option key="2" label="解交叉" value="2" />
                  <el-option key="3" label="主次调换" value="3" />
                </el-select>
              </el-form-item>
            </div>
            <div v-if="$route.name === 3">
              <el-form-item label="商品号">
                <el-input v-model="params.itemNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="UPC">
                <el-input v-model="params.upcNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="供应商编号">
                <el-input v-model="params.vendorNbr" size="mini" placeholder="输入供应商编号搜索" clearable style="width: 200px;" />
              </el-form-item>
            </div>
            <div v-if="$route.name === 4">
              <el-form-item label="部门号">
                <el-input v-model="params.deptNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="PLU#">
                <el-input v-model="params.pluNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
            </div>
            <div v-if="$route.name === 5">
              <el-form-item label="商品号">
                <el-input v-model="params.itemNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="UPC">
                <el-input v-model="params.upcNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="作业类型">
                <el-select v-model="params.stausType" size="mini" placeholder="选择搜索" clearable style="width: 200px">
                  <el-option key="1" label="激活" value="1" />
                  <el-option key="2" label="转状态" value="2" />
                </el-select>
              </el-form-item>
            </div>
            <div v-if="$route.name === 6">
              <el-form-item label="Item Nbr">
                <el-input v-model="params.itemNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="UPC">
                <el-input v-model="params.upcNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="Dept">
                <el-input v-model="params.deptNbr" size="mini" placeholder="输入搜索" clearable style="width: 200px;" />
              </el-form-item>
              <el-form-item label="供应商编号">
                <el-input v-model="params.vendorNbr" size="mini" placeholder="输入供应商编号搜索" clearable style="width: 200px;" />
              </el-form-item>
            </div>
            <el-form-item label="状态">
              <el-select v-model="params.itemStatus" size="mini" placeholder="选择搜索" clearable style="width: 200px">
                <el-option :key="0" label="供应商撤回" :value="0" />
                <el-option :key="1" label="供应商上传待审" :value="1" />
                <el-option :key="2" label="待数据校验" :value="2" />
                <el-option :key="-2" label="已驳回至供应商" :value="-2" />
                <el-option :key="3" label="数据校验中" :value="3" />
                <el-option :key="4" label="数据校验成功" :value="4" />
                <el-option :key="-4" label="数据校验失败" :value="-4" />
                <el-option :key="5" label="Item File审核中" :value="5" />
                <el-option :key="10" label="BPMS审核中" :value="10" />
                <el-option :key="11" label="MDA/BPMS审核中" :value="11" />
                <el-option :key="-6" label="BPMS审核驳回" :value="-6" />
                <el-option :key="-10" label="MDA审核驳回" :value="-10" />
                <el-option :key="-11" label="Item File审核驳回" :value="-11" />
                <el-option :key="100" label="已发送Item File" :value="100" />
                <el-option :key="-1" label="已作废" :value="-1" />
              </el-select>
            </el-form-item>
            <el-form-item label="变更时间">
              <el-date-picker
                v-model="changeTime"
                size="mini"
                type="datetimerange"
                value-format="yyyy/MM/dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="changeTimeChange"
              />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" style="float:right;margin-left:0px;" icon="el-icon-refresh" round @click="resetForm">重置</el-button>
              <el-button size="mini" type="primary" style="float:right;margin-left:0px;" icon="el-icon-search" round @click="toQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div style="padding-top:5px">
      <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-delete" round @click="doDeleteItem">作废</el-button>
      <el-button v-if="$route.name === 1" size="mini" type="danger" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-close" round @click="batchReject">驳回</el-button>
      <el-button v-if="$route.name === 1" size="mini" type="success" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-check" round @click="batchAgree">同意</el-button>
      <!-- <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;" icon="el-icon-connection" @click="doBatchChangeBuyer">批量转交Item</el-button> -->
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-upload" round @click="doSubmitBPMS">提交审核</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-monitor" round @click="doBatchValidate">校验</el-button>
      <el-upload
        v-if="$route.name === 1 || $route.name === 4 || $route.name === 6"
        ref="upload"
        :auto-upload="false"
        :on-change="handleFileChange"
        :show-file-list="false"
        :limit="1"
        :multiple="false"
        action="#"
        accept=""
        style="float:right;margin-bottom:5px;margin-left:0px;"
      >
        <el-button size="mini" type="primary" round icon="el-icon-upload2">上传附件</el-button>
      </el-upload>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-download" round @click="doExportExcel">导出Excel</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-upload2" round @click="doUpload">导入Excel</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-edit" round @click="batchUpdateItem">修改</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-plus" round @click="batchCreateItem">新增</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-refresh-right" round @click="doClearSelection">取消选择</el-button>
      <el-button size="mini" type="primary" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-refresh-right" round @click="toQuery">刷新</el-button>
      <!-- 字段变更 -->
      <el-table
        v-if="$route.name === 1"
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        :height="tableHeight"
        stripe
        size="small"
        border
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="流水号" prop="vendorPortalId" :width="colWidths.vendorPortalId" />
        <el-table-column :show-overflow-tooltip="true" label="商品号" prop="itemNbr" :width="colWidths.itemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'itemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="条码" prop="upcNbr" :width="colWidths.upcNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'upcNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="部门" prop="deptNbr" :width="colWidths.deptNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'deptNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="状态" prop="itemStatusCode" :width="colWidths.itemStatusCode">
          <template slot-scope="scope">{{ getValue(scope.row,'itemStatusCode') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="商品类型" prop="itemTypeCode" :width="colWidths.itemTypeCode">
          <template slot-scope="scope">{{ getValue(scope.row,'itemTypeCode') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="供应商编号" prop="vendorNbr" :width="colWidths.vendorNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'vendorNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="供应商名称" prop="vendorName" :width="colWidths.vendorName">
          <template slot-scope="scope">{{ getValue(scope.row,'vendorName') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变更时间" prop="changeTime" :width="colWidths.changeTime">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'changeTime')">{{ parseTime(getValue(scope.row,'changeTime')) }}</span>
          </template>
        </el-table-column>
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 交叉商品 -->
      <el-table
        v-if="$route.name === 2"
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        :height="tableHeight"
        stripe
        size="small"
        border
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" label="主商品" prop="mainItemNbr" :width="colWidths.mainItemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'mainItemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="次商品" prop="secondItemNbr" :width="colWidths.secondItemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'secondItemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="作业类型" prop="workOption" :width="colWidths.workOption">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'workOption') === '1'">连交叉</span>
            <span v-if="getValue(scope.row,'workOption') === '2'">解交叉</span>
            <span v-if="getValue(scope.row,'workOption') === '3'">主次调换</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变更时间" prop="changeTime" :width="colWidths.changeTime">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'changeTime')">{{ parseTime(getValue(scope.row,'changeTime')) }}</span>
          </template>
        </el-table-column>
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 改Trait -->
      <el-table
        v-if="$route.name === 3"
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        :height="tableHeight"
        stripe
        size="small"
        border
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" label="商品号" prop="itemNbr" :width="colWidths.itemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'itemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="UPC" prop="upcNbr" :width="colWidths.upcNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'upcNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="供应商编号" prop="vendorNbr" :width="colWidths.vendorNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'vendorNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="加送或减送" prop="addOrOmit" :width="colWidths.addOrOmit">
          <template slot-scope="scope">{{ getValue(scope.row,'addOrOmit') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="新发送特号" prop="newSendTrait" :width="colWidths.newSendTrait">
          <template slot-scope="scope">{{ getValue(scope.row,'newSendTrait') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="新不发送特号" prop="newOmitSendTrait" :width="colWidths.newOmitSendTrait">
          <template slot-scope="scope">{{ getValue(scope.row,'newOmitSendTrait') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变更时间" prop="changeTime" :width="colWidths.changeTime">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'changeTime')">{{ parseTime(getValue(scope.row,'changeTime')) }}</span>
          </template>
        </el-table-column>
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 改PLU -->
      <el-table
        v-if="$route.name === 4"
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        :height="tableHeight"
        stripe
        size="small"
        border
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" label="部门号" prop="deptNbr" :width="colWidths.deptNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'deptNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="PLU#" prop="pluNbr" :width="colWidths.pluNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'pluNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变更时间" prop="changeTime" :width="colWidths.changeTime">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'changeTime')">{{ parseTime(getValue(scope.row,'changeTime')) }}</span>
          </template>
        </el-table-column>
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 删除 -->
      <el-table
        v-if="$route.name === 6"
        ref="itemTab"
        :data="data"
        :row-key="getRowKeys"
        :height="tableHeight"
        stripe
        size="small"
        border
        @header-dragend="colChange"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <el-table-column :show-overflow-tooltip="true" label="Item Nbr" prop="itemNbr" :width="colWidths.itemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'itemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="UPC" prop="upcNbr" :width="colWidths.upcNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'upcNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="Dept" prop="deptNbr" :width="colWidths.deptNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'deptNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="供应商编号" prop="vendorNbr" :width="colWidths.itemNbr">
          <template slot-scope="scope">{{ getValue(scope.row,'itemNbr') }}</template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="作业类型" prop="workOption" :width="colWidths.workOption">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'workOption') === '1'">RTV</span>
            <span v-if="getValue(scope.row,'workOption') === '2'">MD</span>
            <span v-if="getValue(scope.row,'workOption') === '3'">Status Dele</span>
            <span v-if="getValue(scope.row,'workOption') === '4'">Permanence Dele</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变更时间" prop="changeTime" :width="colWidths.changeTime">
          <template slot-scope="scope">
            <span v-if="getValue(scope.row,'changeTime')">{{ parseTime(getValue(scope.row,'changeTime')) }}</span>
          </template>
        </el-table-column>
        <itemStatusForm />
        <el-table-column :show-overflow-tooltip="true" sortable="custom" label="最近修改时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        :page-sizes="[10,50,100,500]"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <fieldForm ref="fieldForm" :parent="parent" />
    <ixiuForm ref="ixiuForm" :parent="parent" />
    <traitForm ref="traitForm" :parent="parent" />
    <pluForm ref="pluForm" :parent="parent" />
    <deletionForm ref="deletionForm" :parent="parent" />
    <urgentForm ref="urgentForm" :parent="parent" />
    <uploadExcelForm ref="uploadExcelForm" :parent="parent" />
  </div>
</template>

<script>
import fieldForm from './field'
import ixiuForm from './ixiu'
import traitForm from './trait'
import pluForm from './plu'
import deletionForm from './deletion'
import initData from '@/utils/initData'
import { parseTime } from '@/utils/index'
import { batchChangeBuyer, batchAuditFieldAgree, batchAuditFieldReject, submitBPMS, batchValidate, batchUpdateFilePath, uploadFile } from '@/api/change-item'
import { Message } from 'element-ui'
import urgentForm from './urgent'
import itemStatusForm from '@/views/part/itemStatus'
import { getBuyerIdList } from '@/api/api'
import { doDownload } from '@/api/download'
import uploadExcelForm from './uploadExcel'
import { getFileNameByType } from '@/utils/item'
import { doDeleteItem } from '@/utils/changeItem'

export default {
  components: { fieldForm, ixiuForm, traitForm, pluForm, deletionForm, urgentForm, itemStatusForm, uploadExcelForm },
  mixins: [initData],
  data() {
    return {
      changeTime: [],
      parent: this,
      ids: [],
      itemList: [],
      loading: false,
      tableHeight: window.innerHeight * 0.65,
      auditBuyerId: '',
      activeName: '1'
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    doDeleteItem,
    beforeInit() {
      this.url = '/change-item/changeItemList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      this.params.changeType = this.$route.name
      return true
    },
    getValue(row, key) { // 获取信息
      if (row === undefined || row === null || key === undefined || key === null || key === '') {
        return ''
      }
      if (row.itemInfo === undefined || row.itemInfo === null) {
        return ''
      }
      var item = JSON.parse(row.itemInfo)
      if (item === null) {
        return ''
      }
      return item[key]
    },
    // 上传时间日期组件触发
    changeTimeChange(val) {
      this.params.findChangeTimeStartDate = val[0]
      this.params.findChangeTimeEndDate = val[1]
    },
    // 列表排序触发
    sortChange(val) {
      let sortType = ''
      if (val.order === 'descending') {
        sortType = ' DESC'
      } else if (val.order === 'ascending') {
        sortType = ' ASC'
      }
      this.params.sort = val.prop + sortType
      this.init()
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
      this.doClearSelection()
    },
    // 查询栏重置触发
    resetForm() {
      this.params = {}
      this.changeTime = []
    },
    // 列表多选框触发
    handleSelectionChange(val) {
      this.ids = []
      this.itemList = []
      val.forEach(item => {
        this.ids.push(item.id)
        var itemInfo = JSON.parse(item.itemInfo)
        itemInfo.id = item.id
        this.auditBuyerId = item.buyerId
        this.itemList.push(itemInfo)
      })
    },
    // 翻页选择row的key值
    getRowKeys(row) {
      return row.id
    },
    // 批量修改触发
    batchUpdateItem() {
      if (this.itemList.length < 1) {
        Message({
          message: '请选择需要修改的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else if (this.itemList.length > 100) {
        Message({
          message: '编辑数据过多（最大100），请使用Excel导入方式进行操作。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.loading = true
        let _this = null
        switch (this.$route.name) {
          case 1:
            _this = this.$refs.fieldForm
            break
          case 2:
            _this = this.$refs.ixiuForm
            break
          case 3:
            _this = this.$refs.traitForm
            break
          case 4:
            _this = this.$refs.pluForm
            break
          case 6:
            _this = this.$refs.deletionForm
            break
          default:
            return
        }
        _this.colWidthKey = this.$route.name + 'edit'
        _this.getTableColWidth()
        _this.data = _this.data.concat(this.itemList)
        _this.dialog = true
        _this.createFlag = false
        this.loading = false
      }
    },
    // 批量新增触发
    batchCreateItem() {
      this.loading = true
      getBuyerIdList().then(val => {
        let _this = null
        switch (this.$route.name) {
          case 1:
            _this = this.$refs.fieldForm
            break
          case 2:
            _this = this.$refs.ixiuForm
            break
          case 3:
            _this = this.$refs.traitForm
            break
          case 4:
            _this = this.$refs.pluForm
            break
          case 6:
            _this = this.$refs.deletionForm
            break
          default:
            return
        }
        _this.colWidthKey = this.$route.name + 'edit'
        _this.getTableColWidth()
        _this.buyerList = val
        _this.buyerId = val[0]
        _this.data = []
        _this.dialog = true
        _this.createFlag = true
        this.loading = false
      }, error => {
        console.log(error)
        this.loading = false
      })
    },
    // 批量转交Item
    doBatchChangeBuyer() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要转交的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$prompt('请输入需转交处理人AD账号', 'Item转交', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((changeBuyerId) => {
          batchChangeBuyer(this.ids, changeBuyerId.value).then(res => {
            this.toQuery()
            Message({
              message: '转交成功。',
              type: 'success',
              duration: 3 * 1000
            })
          })
        })
      }
    },
    // 批量同意触发
    batchAgree() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要审核的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认审核通过选择的 ' + this.ids.length + ' 条item信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          batchAuditFieldAgree(this.ids).then(res => {
            this.toQuery()
            Message({
              message: '审核完成。',
              type: 'success',
              duration: 3 * 1000
            })
          })
        })
      }
    },
    // 批量审核驳回
    batchReject() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要审核的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$prompt('请输入驳回原因', '驳回 ' + this.ids.length + ' 条Item', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValidator: this.rejectRemarkValidator,
          type: 'warning'
        }).then((rejectRemark) => {
          batchAuditFieldReject(this.ids, rejectRemark.value).then(res => {
            this.toQuery()
            Message({
              message: '审核完成。',
              type: 'success',
              duration: 3 * 1000
            })
          })
        })
      }
    },
    rejectRemarkValidator(val) {
      if (!val) {
        return '请填写驳回原因'
      } else if (val.length > 500) {
        return '驳回原因长度为1-500'
      }
      return true
    },
    // 清空选择项
    doClearSelection() {
      this.$refs.itemTab.clearSelection()
    },
    // 批量提交审核前置
    doSubmitBPMS() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要提交审核的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else if (this.ids.length > 500) {
        Message({
          message: '提交信息过多（不超过500条）',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        const _this = this.$refs.urgentForm
        _this.itemSize = this.ids.length
        _this.changeType = this.$route.name
        _this.bpmsParam.auditBuyerId = this.auditBuyerId
        _this.ids = this.ids
        _this.dialog = true
      }
    },
    // 提交审核操作
    submitBPMS(urgent, bpmsParam) {
      this.loading = true
      submitBPMS({ urgentFlag: urgent, changeType: this.$route.name, bpmsParamDTO: bpmsParam, ids: this.ids }).then(res => {
        this.toQuery()
        Message({
          message: '数据提交成功。',
          type: 'success',
          duration: 3 * 1000
        })
      }, (error) => {
        console.log(error)
        this.loading = false
      })
    },
    // 批量校验
    doBatchValidate() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要校验的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认校验选择的 ' + this.ids.length + ' 条item信息?', '校验数据', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          batchValidate({ ids: this.ids, changeType: this.$route.name }).then(res => {
            this.toQuery()
            Message({
              message: '校验处理中，请稍后刷新页面进行查询。',
              type: 'success',
              duration: 3 * 1000
            })
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    handleFileChange(file, fileList) {
      const sux = file.name.substring(file.name.lastIndexOf('.'))
      var size = file.size / 1024 / 1024
      if (this.$route.name === 6 && (sux !== '.xlsx' && sux !== '.xlsm')) {
        Message({
          message: '删除表单只能上传Excel文件（xlsx/xlsm）。',
          type: 'warning',
          duration: 3 * 1000
        })
        fileList.pop()
      } else if (size > 5) {
        Message({
          message: '文件大小必须小于5M。',
          type: 'warning',
          duration: 3 * 1000
        })
        fileList.pop()
      } else if (this.ids.length < 1) {
        Message({
          message: '请选择需要上传附件的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('状态为 待数据校验/数据校验失败 方可上传附件，附件上传会覆盖原有附件，是否上传？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          var formData = new FormData()
          formData.append('file', file.raw)
          uploadFile(formData).then(res => {
            if (res) {
              const filePath = res[0].fileUrl
              if (filePath) {
                batchUpdateFilePath({ ids: this.ids, filePath: filePath }).then(() => {
                  this.toQuery()
                  Message({
                    message: '上传成功。',
                    type: 'success',
                    duration: 3 * 1000
                  })
                  this.loading = false
                }, error => {
                  console.log(error)
                  this.loading = false
                })
              }
            }
          }, error => {
            console.log(error)
            this.loading = false
          })
          fileList.pop()
        })
      }
    },
    // 导出操作
    doExportExcel() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要导出的数据。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认导出选择的 ' + this.ids.length + ' 条item信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          doDownload('/change-item/exportExcel', { ids: this.ids, changeType: this.$route.name }, getFileNameByType(this.$route.name)).then(() => {
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    // 导入触发
    doUpload() {
      this.loading = true
      getBuyerIdList().then(val => {
        const _this = this.$refs.uploadExcelForm
        _this.buyerList = val
        _this.buyerId = val[0]
        _this.changeType = this.$route.name
        _this.dialog = true
        this.loading = false
      }, error => {
        console.log(error)
        this.loading = false
      })
    }
  }
}
</script>
